package com.luych.serviceGatewat.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.util.ZuulRuntimeException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class AccessFilter extends ZuulFilter{

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        //RequestContext context = RequestContext.getCurrentContext();
        //HttpServletRequest request = context.getRequest();

        //logger.info(request.getRequestURL().toString());

        //context.setSendZuulResponse(false);
        //context.setResponseStatusCode(401);

        //异常处理
        /*context.set("error.status_code", 500);
        context.set("error.exception", new RuntimeException("12333"));
        context.set("error.message", "错误描述");*/

        return null;
    }

    @Override
    public String filterType() {
        // pre 请求之前被调用
        // routing 请求时被调用
        // error 处理请求发生错误时被调用
        // post 在routing和error之后被调用
        return "pre";
    }

    @Override
    public int filterOrder() {
        // 数值越小优先级越高
        return 0;
    }
}
